package com.zx.sdk.league.member;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.shu.priory.IFLYAdSDK;
import com.shu.priory.IFLYSplashAd;
import com.shu.priory.IFLYVideoAd;
import com.shu.priory.config.AdError;
import com.shu.priory.config.AdKeys;
import com.shu.priory.conn.SplashDataRef;
import com.shu.priory.listener.IFLYSplashListener;
import com.zx.sdk.BuildConfig;
import com.zx.sdk.ZxSDK;
import com.zx.sdk.listener.BaseZxRewardListener;
import com.zx.sdk.listener.BaseZxSplashListener;
import com.zx.sdk.model.AdInfo;
import com.zx.sdk.model.ReadyAdPosition;
import com.zx.sdk.model.ZxError;
import com.zx.sdk.util.LogHelper;

import java.util.HashMap;

public class XunFei extends BaseLeagueMember<IFLYSplashAd, IFLYVideoAd, Object, Object, Object> {
    private final HashMap<String, SplashDataRef> splashDataRefMap = new HashMap<>();

    @Override
    public String getName() {
        return "xunfei";
    }

    @Override
    protected void onInit(Context context, String appId, boolean initialized) {
        IFLYAdSDK.init(context);
    }

    @Override
    protected void onLoadSplash(AdInfo adInfo, ViewGroup container, String mode, String type, BaseZxSplashListener listener) {
        String mapPid = adInfo.getMapPid();

        splashDataRefMap.remove(mapPid);
        //创建开屏广告：adUnitId：开发者在讯飞AINativeSplashAdActivity销云平台(http://www.voiceads.cn/)申请的开屏广告位 ID
        IFLYSplashAd[] splashAd = new IFLYSplashAd[1];
        splashAd[0] = new IFLYSplashAd(container.getContext(), mapPid, new IFLYSplashListener() {
            @Override
            public void onAdLoaded(SplashDataRef splashDataRef) {
                splashAd[0].setParameter(AdKeys.AUCTION_PRICE, splashDataRef.getPrice());
                saveSplash(mapPid, splashAd[0], container);
                splashDataRefMap.put(mapPid, splashDataRef);
                if (ZxSDK.FETCH_AND_SHOW.equals(mode)) {
                    if (!showSplash(adInfo, container, listener)) {
                        listener.onNoAD(XunFei.this, new ZxError("-1", "广告显示失败"), adInfo);
                        return;
                    }
                }
                listener.onADLoaded(XunFei.this, 0, adInfo);
            }

            @Override
            public void onAdFailed(AdError adError) {
                switch (mode) {
                    case ZxSDK.FETCH_ONLY:
                        listener.onPreLoadNoAD(XunFei.this, new ZxError(adError.getLocalizedMessage(), adError.getErrorDescription()), adInfo);
                        break;
                    case ZxSDK.FETCH_AND_SHOW:
                        listener.onNoAD(XunFei.this, new ZxError(adError.getLocalizedMessage(), adError.getErrorDescription()), adInfo);
                        break;
                }
            }

            @Override
            public void onConfirm() {
            }

            @Override
            public void onCancel() {
            }

            @Override
            public void onDownloading() {
                // 下载类广告，重复下载时回调
            }

            @Override
            public void onAdExposure() {
                listener.onADExposure(XunFei.this, adInfo);
            }

            @Override
            public void onAdSkip() {
                listener.onADDismissed(XunFei.this, adInfo);
            }

            @Override
            public void onAdClick() {
                listener.onADClicked(XunFei.this, adInfo);
            }

            @Override
            public void onAdTimeOver() {
                listener.onADDismissed(XunFei.this, adInfo);
            }
        });
        //设置oaid，获取方式见 -> 4.常见问题
        splashAd[0].setParameter(AdKeys.OAID, ZxSDK.oaid);
        //设置倒计时秒数，默认5秒，有效范围3-5，单位秒
        splashAd[0].setParameter(AdKeys.COUNT_DOWN, 5);
        //设置是否开启下载确认
        splashAd[0].setParameter(AdKeys.DOWNLOAD_ALERT, true);
        //开启debug模式
        splashAd[0].setParameter(AdKeys.DEBUG_MODE, BuildConfig.DEBUG);
        //若使用rtb竞价，需通过此接口设置底价；固价模式无需设置
        splashAd[0].setParameter(AdKeys.SETTLE_TYPE, "1");
        splashAd[0].setParameter(AdKeys.BID_FLOOR, 1.0);
        //设置关闭摇一摇和上滑，仅支持点击广告跳转
        //mSplashAd.setParameter(AdKeys.SPLASH_INTERACTION_DISABLE, true);
        splashAd[0].loadAd();
    }

    @Override
    public boolean onShowSplash(AdInfo adInfo, ViewGroup container, @Nullable IFLYSplashAd splash, BaseZxSplashListener listener) {
        if (splash == null) return false;
        splash.showAd(container);
        return true;
    }

    @Override
    public void onLoadReward(Activity context, AdInfo adInfo, String mode, String type, BaseZxRewardListener listener) {

    }

    @Override
    public boolean onShowReward(Activity context, AdInfo adInfo, @Nullable IFLYVideoAd reward, BaseZxRewardListener listener) {
        return false;
    }

    @Override
    public void onLoadInterstitial(Activity context, AdInfo adInfo, String mode, String type, BaseZxRewardListener listener) {

    }

    @Override
    public boolean onShowInterstitial(Activity context, AdInfo adInfo, @Nullable Object interstitial, BaseZxRewardListener listener) {
        return false;
    }

    @Override
    public int getCpmByPid(String type, String pid) {
        int cpm = super.getCpmByPid(type, pid);
        try {
            switch (type) {
                case ZxSDK.SPLASH:
                    if (splashMap.containsKey(pid)) {
                        cpm = (int) (splashDataRefMap.get(pid).getPrice() * 100F);
                    }
            }
        } catch (Exception exception) {
            LogHelper.d("XunFei getCpmByPid exception " + exception.getMessage());
        }
        cpmCache.put(type + pid, cpm);
        return cpm;
    }

    @Override
    public boolean isPreResReady(String type, String pid) {
        switch (type) {
            case ZxSDK.SPLASH:
                return splashMap.get(pid) != null;
            case ZxSDK.REWARD:
                return rewardMap.get(pid) != null;
        }
        return false;
    }

    @Override
    public void notifyBiddingWin(String type, String mapPid, @Nullable ReadyAdPosition loser) throws Exception {
        SplashDataRef splashDataRef = splashDataRefMap.get(mapPid);
        if (splashDataRef != null) {
            splashDataRef.onBiddingSuccess();
        }
        LogHelper.d(getName(), "notifyBiddingWin", mapPid, "selfPrice =" + getCpmByPid(type, mapPid), "LoserPrice = " + (loser != null ? loser.getCpm() : 0));
    }

    @Override
    public void notifyBiddingLose(String type, String mapPid, ReadyAdPosition winner, ZxSDK.LoseReason reason) throws Exception {
        SplashDataRef splashDataRef = splashDataRefMap.get(mapPid);
        if (splashDataRef != null) {
            splashDataRef.onBiddingFailure(AdKeys.BiddingFailureType.TYPE_LOW_PRICE, "");
        }
        LogHelper.d(getName(), "notifyBiddingLose", mapPid, "selfPrice =" + getCpmByPid(type, mapPid), "WinnerPrice = " + (winner != null ? winner.getCpm() : 0));
    }

    @Override
    public void destroySplash(Activity activity) {

    }

    @Override
    public void destroyShownSplash(Activity activity, IFLYSplashAd splashAd) {

    }

    @Override
    public void destroyReward(Activity activity) {

    }

    @Override
    public void destroyShownReward(Activity activity, IFLYVideoAd rewardAd) {

    }
}
